package funtastic.spellingbee;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.media.AudioManager;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class CustomSpellingsActivity extends Activity
{
    private static final int DIALOG_CREATE_CUSTOM_SPELLING = 0;
    private static final int DIALOG_UPDATE_CUSTOM_SPELLING = 1;

    private String           mCurrentCustomSpelling;
    private String           mCurrentCustomSpellingList;
    private ListView         mCustomSpellings;
    private EditText         mEditCustomSpelling;
    private EditText         mEditCustomSpellingSentence;
    public OnItemClickListener    mItemClickListener = new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            mSpellingBee.getTextToSpeech().speak(((TextView) view).getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
        }
    };
    private EditText         mNewCustomSpelling;
    private EditText         mNewCustomSpellingSentence;
    private SpellingBee      mSpellingBee;

    @Override
    public boolean onContextItemSelected(MenuItem item)
    {
        if (item.getItemId() == R.id.menuItemEditCustomSpelling)
        {
            showDialog(DIALOG_UPDATE_CUSTOM_SPELLING);
        }
        else if (item.getItemId() == R.id.menuItemDeleteCustomSpelling)
        {
            AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle("Confirm Custom Spelling List Deletion").setMessage("Are you sure you want to delete " + mCurrentCustomSpelling + "?").setCancelable(false)
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener()
                    {
                        @Override
                        public void onClick(DialogInterface dialog, int id)
                        {
                            mSpellingBee.deleteCustomSpelling(mCurrentCustomSpellingList, mCurrentCustomSpelling);

                            refreshCustomSpellings();
                        }
                    }).setNegativeButton("No", new DialogInterface.OnClickListener()
                    {
                        @Override
                        public void onClick(DialogInterface dialog, int id)
                        {
                            dialog.cancel();
                        }
                    }).create();
            alertDialog.show();
        }
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.custom_activity);
        setVolumeControlStream(AudioManager.STREAM_MUSIC);

        mCurrentCustomSpelling = "";
        mCurrentCustomSpellingList = getIntent().getExtras().getString("CustomSpellingList");
        mCustomSpellings = (ListView) findViewById(R.id.listViewItems);
        mSpellingBee = (SpellingBee) getApplication();

        mCustomSpellings.setAdapter(new ArrayAdapter<SpellingDetails>(this, R.layout.list_item_practice, mSpellingBee.getCustomSpellings(mCurrentCustomSpellingList)));
        mCustomSpellings.setOnItemClickListener(mItemClickListener);

        registerForContextMenu(mCustomSpellings);
        setTitle(getString(R.string.app_name) + " - Custom Spellings - " + mCurrentCustomSpellingList);
    }

    @Override
    public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenuInfo menuInfo)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_custom_spelling, contextMenu);

        AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        mCurrentCustomSpelling = ((TextView) info.targetView).getText().toString();
        ((TextView) info.targetView).getText().toString();
        contextMenu.setHeaderTitle(mCurrentCustomSpelling);
    }

    @Override
    protected Dialog onCreateDialog(int id)
    {
        LayoutInflater factory = LayoutInflater.from(this);

        switch (id)
        {
            case DIALOG_CREATE_CUSTOM_SPELLING:
                final View newCustomSpellingList = factory.inflate(R.layout.new_custom_spelling, null);
                mNewCustomSpelling = (EditText) newCustomSpellingList.findViewById(R.id.editTextNewCustomSpelling);
                mNewCustomSpellingSentence = (EditText) newCustomSpellingList.findViewById(R.id.editTextNewCustomSentence);
                Dialog newCustomSpellingDialog = new AlertDialog.Builder(this).setIcon(R.drawable.ic_menu_agenda).setTitle(R.string.add_custom_spelling).setView(newCustomSpellingList)
                        .setPositiveButton(R.string.save, new DialogInterface.OnClickListener()
                        {
                            @Override
                            public void onClick(DialogInterface dialog, int id)
                            {
                                String errorMessage = "";
                                
                                if (mNewCustomSpelling.getText().toString().trim().length() == 0)
                                {
                                    errorMessage = "Please enter a valid Spelling and try again.";
                                    showMessage(errorMessage);
                                }
                                else if (mNewCustomSpellingSentence.getText().toString().trim().length() == 0)
                                {
                                    errorMessage = "Please enter a valid Spelling Hint and try again.";
                                    showMessage(errorMessage);
                                }
                                else
                                {
                                    errorMessage = mSpellingBee.addCustomSpelling(mCurrentCustomSpellingList, mNewCustomSpelling.getText().toString(), mNewCustomSpellingSentence.getText().toString());

                                    if (errorMessage.length() == 0)
                                    {
                                        refreshCustomSpellings();
                                    }
                                    else
                                    {
                                        showMessage(errorMessage);
                                    }
                                }
                            }
                        }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener()
                        {
                            @Override
                            public void onClick(DialogInterface dialog, int id)
                            {
                                dialog.cancel();
                            }
                        }).create();
                newCustomSpellingDialog.setOnShowListener(new DialogInterface.OnShowListener()
                {
                    @Override
                    public void onShow(DialogInterface arg0)
                    {
                        mNewCustomSpelling.setText("");
                        mNewCustomSpellingSentence.setText("");
                    }
                });
                return newCustomSpellingDialog;

            case DIALOG_UPDATE_CUSTOM_SPELLING:
                final View editCustomSpelling = factory.inflate(R.layout.new_custom_spelling, null);
                mEditCustomSpelling = (EditText) editCustomSpelling.findViewById(R.id.editTextNewCustomSpelling);
                mEditCustomSpellingSentence = (EditText) editCustomSpelling.findViewById(R.id.editTextNewCustomSentence);
                Dialog updateCustomSpellingDialog = new AlertDialog.Builder(this).setIcon(R.drawable.ic_menu_agenda).setTitle(R.string.edit_custom_spelling).setView(editCustomSpelling)
                        .setPositiveButton(R.string.save, new DialogInterface.OnClickListener()
                        {
                            @Override
                            public void onClick(DialogInterface dialog, int id)
                            {
                                dialog.cancel();

                                String errorMessage = "";
                                
                                if (mEditCustomSpelling.getText().toString().trim().length() == 0)
                                {
                                    errorMessage = "Please enter a valid Spelling and try again.";
                                    showMessage(errorMessage);
                                }
                                else if (mEditCustomSpellingSentence.getText().toString().trim().length() == 0)
                                {
                                    errorMessage = "Please enter a valid Spelling Hint and try again.";
                                    showMessage(errorMessage);
                                }
                                else if (mCurrentCustomSpelling.equals(mEditCustomSpelling.getText().toString()))
                                {
                                    errorMessage = mSpellingBee.updateCustomSpellingSentence(mCurrentCustomSpellingList, mCurrentCustomSpelling, mEditCustomSpellingSentence.getText().toString());
                                }
                                else
                                {
                                    errorMessage = mSpellingBee.updateCustomSpelling(mCurrentCustomSpellingList, mCurrentCustomSpelling, mEditCustomSpelling.getText().toString(), mEditCustomSpellingSentence.getText().toString());
                                }

                                if (errorMessage.length() == 0)
                                {
                                    refreshCustomSpellings();
                                }
                                else
                                {
                                    showMessage(errorMessage);
                                }
                            }
                        }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener()
                        {
                            @Override
                            public void onClick(DialogInterface dialog, int id)
                            {
                                dialog.cancel();
                            }
                        }).create();
                updateCustomSpellingDialog.setOnShowListener(new DialogInterface.OnShowListener()
                {
                    @Override
                    public void onShow(DialogInterface arg0)
                    {
                        mEditCustomSpelling.setText(mCurrentCustomSpelling);

                        mEditCustomSpellingSentence.setText(mSpellingBee.getCustomSpellingSentence(mCurrentCustomSpellingList, mCurrentCustomSpelling));
                    }
                });
                return updateCustomSpellingDialog;

            default:
                break;
        }
        return super.onCreateDialog(id);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_new_custom_spelling_list, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.itemNew:
                showDialog(DIALOG_CREATE_CUSTOM_SPELLING);
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public void refreshCustomSpellings()
    {
        mCustomSpellings.setAdapter(new ArrayAdapter<SpellingDetails>(this, R.layout.list_item_practice, mSpellingBee.getCustomSpellings(mCurrentCustomSpellingList)));
    }

    public void showMessage(String message)
    {
        AlertDialog alertDialog = new AlertDialog.Builder(this).setMessage(message).setCancelable(false).setPositiveButton("Ok", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int id)
            {
                dialog.cancel();
            }
        }).create();
        alertDialog.show();
    }
}
